#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include "CIpodLog.h"
#include "CIpodVehicle.h"

uint8 CIpodVehicle::s_BtAddress[6];
bool CIpodVehicle::s_BtConnected = true;
std::string CIpodVehicle::s_CachePath;
WifiConfigurationInfo CIpodVehicle::s_WifiInformarion;
BtSppState CIpodVehicle::s_BTSppConnStatus = ipod_SppState_ReadyNoInitialized;
WiFiState CIpodVehicle::s_WiFiConnStatus = WIFI_NOCONNECT;

void CIpodVehicle::initCachePath() {
    char const *cachePath = "/sdcard/"; //getenv("IPOD_CACHE_FILE_PATH");
//    strcpy(cachePath, "/usb1/ipodconfig2.xml");
    if (cachePath != NULL) {
        if (access(cachePath, 0) != 0) {
            mkdir(cachePath, 0777);
        }
        s_CachePath = cachePath;
    }
    CIpodLog::i("CIpodVehicle::initCachePath, s_CachePath = %s\n", s_CachePath.c_str());
}

const char *CIpodVehicle::getCachePath() {
    return s_CachePath.c_str();
}

void CIpodVehicle::setBtAddress(const uint8 *address) {
    if (address != NULL)
    {
        char addrlog[128];
        sprintf(addrlog, "%2x:%2x:%2x:%2x:%2x:%2x", address[0], address[1], address[2], address[3], address[4], address[5]);
        // CIpodLog::i("CIpodVehicle::setBtAddress, address is %s!\n", addrlog);

        memcpy(s_BtAddress, address, sizeof(s_BtAddress));
    }
}

const uint8 *CIpodVehicle::getBtAddress() {
    return s_BtAddress;
}

void CIpodVehicle::setBtStatus(bool connected) {
    s_BtConnected = connected;
}

bool CIpodVehicle::getBtStatus() {
    return s_BtConnected;
}

BtSppState CIpodVehicle::getbtSppConnStatus() {
    return s_BTSppConnStatus;
}

void CIpodVehicle::setbtSppConnStatus(BtSppState btSppConnStatus){
    CIpodLog::d("btSppConnStatus = %d\n", btSppConnStatus);
    s_BTSppConnStatus = btSppConnStatus;
}

WiFiState CIpodVehicle::getWiFiConnStatus() {
    return s_WiFiConnStatus;
}

void CIpodVehicle::setWiFiConnStatus(WiFiState wifiConnStatus){
    CIpodLog::d("wifiConnStatus = %d\n", wifiConnStatus);
    s_WiFiConnStatus = wifiConnStatus;
}

WifiConfigurationInfo *CIpodVehicle::getWifiInformarion() {
    return &s_WifiInformarion;
}

void CIpodVehicle::setWifiInformarion(WifiConfigurationInfo wifi_info)
{
    s_WifiInformarion = wifi_info;
    CIpodLog::d("wifi_ssid = %s\n", s_WifiInformarion.wifi_ssid);
    CIpodLog::d("pass_phrase = %s\n", s_WifiInformarion.pass_phrase);
    CIpodLog::d("security_type = %d\n", s_WifiInformarion.security_type);
    CIpodLog::d("channel = %d\n", s_WifiInformarion.channel);
}
